package study.datastructure.stack.leetcode;

import org.junit.platform.commons.util.StringUtils;
import study.datastructure.stack.ArrayStack;
import study.datastructure.stack.Stack;


/**
 * 后缀表达式 字符串数组 不好判断是否只有一个数字
 */
public class Client120 {

    public static int evaluatePostfix(String[] tokens) {
        Stack<Integer> stack = new ArrayStack<>();

        for (int i = 0; i < tokens.length; i++) {
            String token = tokens[i];

                switch (token) {
                    case "+": {
                        int operand2 = stack.pop();
                        int operand1 = stack.pop();
                        stack.push(operand1 + operand2);
                        break;
                    }
                    case "-": {
                        int operand2 = stack.pop();
                        int operand1 = stack.pop();
                        stack.push(operand1 - operand2);
                        break;
                    }
                    case "*": {
                        int operand2 = stack.pop();
                        int operand1 = stack.pop();
                        stack.push(operand1 * operand2);
                        break;
                    }
                    case "/": {
                        int operand2 = stack.pop();
                        int operand1 = stack.pop();
                        stack.push(operand1 / operand2);
                        break;
                    }
                    default:stack.push(Integer.parseInt(token));
                }
            }
        return stack.pop();
    }

    public static void main(String[] args) {

    }
}